package com.example.informationEntry.service.impl;

import com.example.informationEntry.dao.NaSemesterTableDao;
import com.example.informationEntry.entity.NaSemesterTable;
import com.example.informationEntry.service.NaSemesterTableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学期表(NaSemesterTable)表服务实现类
 *
 * @author makejava
 * @since 2019-04-08 11:20:34
 */
@Service("naSemesterTableService")
public class NaSemesterTableServiceImpl implements NaSemesterTableService {
    @Resource
    private NaSemesterTableDao naSemesterTableDao;

    /**
     * 通过ID查询单条数据
     *
     * @param semesterNumber 主键
     * @return 实例对象
     */
    @Override
    public NaSemesterTable queryById(String semesterNumber) {
        return this.naSemesterTableDao.queryById(semesterNumber);
    }


    /**
     * 查询多条数据
     *
     * @param naSemesterTable 实例对象
     * @return list集合
     */
    @Override
    public List<NaSemesterTable> queryAll(NaSemesterTable naSemesterTable){
        return this.naSemesterTableDao.queryAll(naSemesterTable);
    }
    /**
     * 查询所有数据
     *
     * @return 对象列表
     */
    @Override
    public List<NaSemesterTable> queryAllByName(String semesterName) {
        return this.naSemesterTableDao.queryAllByName(semesterName);
    }

    /**
     * 新增数据
     *
     * @param naSemesterTable 实例对象
     * @return 实例对象
     */
    @Override
    public NaSemesterTable insert(NaSemesterTable naSemesterTable) {
        this.naSemesterTableDao.insert(naSemesterTable);
        return naSemesterTable;
    }

    /**
     * 修改数据
     *
     * @param naSemesterTable 实例对象
     * @return 实例对象
     */
    @Override
    public NaSemesterTable update(NaSemesterTable naSemesterTable) {
        this.naSemesterTableDao.update(naSemesterTable);
        return this.queryById(naSemesterTable.getSemesterNumber());
    }

    /**
     * 通过主键删除数据
     *
     * @param semesterNumber 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String semesterNumber) {
        return this.naSemesterTableDao.deleteById(semesterNumber) > 0;
    }

    /**
     * 进行数据规整为 K 学期ID  V 学期name
     *
     * @return 规整好的数据
     */
    @Override
    public Map<String,String> queryNumberToName(){
        Map<String,String> resultMap = new HashMap<>();
        List<NaSemesterTable> naSemesterTables = this.queryAll(new NaSemesterTable());
        for (NaSemesterTable table : naSemesterTables) {
            resultMap.put(table.getSemesterNumber(),table.getSemesterName());
        }
        return resultMap;
    }
}